/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


class LiczbaException extends Exception{};

public class Liczba {
    private int n;
    public char tab[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G'};
    public int r;
    Liczba() {
    }

    Liczba(int n) {
        this.n=n;
    }
    
    public int Value(String l)  {
        int i;
        for(i=0;i<=16;i++)
        {
            if(l.equals(Character.toString(tab[i])))
            {
                break;
            }
        }
        return i;
    };
    
    Liczba(String n, int podstawa) throws LiczbaException  {
        if(podstawa<0 || podstawa>=16) throw new LiczbaException();
        
        int p=1, x;
        int suma=0;
        for(int z=n.length()-1;z>=0;z--)
        {

            x=Value(n.substring(z,z+1));
            
            suma+=(x*p);
            p=p*podstawa;
        }
        this.n=suma;
    }

    public String zapis(int podstawa)  {
        String re="";
        int d=this.n;
        while(d>0)
        {
            re=tab[d%podstawa]+re;
            d=d/podstawa;
        }
        return re;
    };    
}
